<?php

namespace App\Repositorys;

use Illuminate\Support\Facades\DB;
use App\Person;
use App\User;
use App\Province;


class HomeRepository
{
    protected $model;

    protected $user;

    public function __construct(Person $person, User $user)
    {
        $this->model = $person;
        $this->user = $user;
    }

    /**
     * 每年新增人员
     *
     * @return array
     */
    public function years()
    {
        $model1 = $this->model;
        $model2 = $this->model;

        // 获取所有年份
        $first = $model1->orderBy("five_saw_time", "asc")
            ->select('five_saw_time')
            ->where('is_deleted', 0)
            ->where('five_saw_time', '!=', '')
            ->first();

        $last = $model2->orderBy("five_saw_time", "desc")
            ->select('five_saw_time')
            ->where('is_deleted', 0)
            ->where('five_saw_time', '!=', '')
            ->first();

        $first_year = '';
        $last_year = '';
        if ($first) {
            $first_year = date('Y', $first->five_saw_time);
        }
        if ($last) {
            $last_year = date('Y', $last->five_saw_time);
        }

        $year_array = array();
        if ($first_year != '' && $last_year != '') {
            $count = $last_year - $first_year;
            for ($count; $first_year <= $last_year; $first_year++) {
                $year_array[] = (int)$first_year;
            }
        }

        $year_archives = [];
        foreach ($year_array as $k => $v) {
            $start_time = strtotime("$v-1-1 00:00:00");
            $end_time = strtotime("$v-12-31 23:59:59");
            $year_count = Person::select(\DB::raw("count(id) as id_count"))
                ->where('five_saw_time', '>=', $start_time)
                ->where('five_saw_time', '<=', $end_time)
                ->first();
            //var_dump($year_count);
            $year_count = explode(":", $year_count);
            $year_count[1] = str_replace('}', '', $year_count[1]);
            $year_archives[$k]['year'] = $v;
            $year_archives[$k]['count'] = $year_count[1];
        }

        unset($model1, $model2, $year_count, $year_array);

        return $year_archives;
    }

    /**
     * 获取人员户籍地分布信息
     *
     * @return array
     */
    public function regAddr()
    {
        $model = $this->model;

        $reg_addrs_raw = $this->model->select(DB::raw('count(*) as id_count, reg_addr'))
            ->where('is_deleted', 0)
            ->groupBy('reg_addr')->get();
        $reg_addrs = [];

        foreach ($reg_addrs_raw as $k => $v) {
            $v->reg_addr = explode(',', $v->reg_addr)[0];
            $v->reg_addr = str_replace('省', '', $v->reg_addr);
            $v->reg_addr = str_replace('回族', '', $v->reg_addr);
            $v->reg_addr = str_replace('维吾尔', '', $v->reg_addr);
            $v->reg_addr = str_replace('市', '', $v->reg_addr);
            $v->reg_addr = str_replace('壮族', '', $v->reg_addr);
            $v->reg_addr = str_replace('自治区', '', $v->reg_addr);
            $v->reg_addr = str_replace('特别行政区', '', $v->reg_addr);

            if (isset($reg_addrs[$v->reg_addr])) {
                $reg_addrs[$v->reg_addr] += $v->id_count;
            } else {
                $reg_addrs[$v->reg_addr] = $v->id_count;
            }
        }

        unset($model, $reg_addrs_raw);

        return $reg_addrs;
    }

    /**
     * 人员暂住地分布信息
     *
     * @return array
     */
    public function tempAddr()
    {
        $model = $this->model;
        $temp_addrs_raw = $model->select(DB::raw('count(*) as id_count, temp_addr'))
            ->where('is_deleted', 0)
            ->groupBy('temp_addr')->get();
        $temp_addrs = [];

        foreach ($temp_addrs_raw as $k => $v) {
            $v->temp_addr = explode(',', $v->temp_addr)[0];
            $v->temp_addr = str_replace('省', '',  $v->temp_addr);
            $v->temp_addr = str_replace('回族', '',  $v->temp_addr);
            $v->temp_addr = str_replace('维吾尔', '',  $v->temp_addr);
            $v->temp_addr = str_replace('市', '',  $v->temp_addr);
            $v->temp_addr = str_replace('壮族', '',  $v->temp_addr);
            $v->temp_addr = str_replace('自治区', '',  $v->temp_addr);
            $v->temp_addr = str_replace('特别行政区', '',  $v->temp_addr);
            if (isset($temp_addrs[$v->temp_addr])) {
                $temp_addrs[$v->temp_addr] += $v->id_count;
            } else {
                $temp_addrs[$v->temp_addr] = $v->id_count;
            }
        }

        // 获取人员暂住地地理分布图
        $temp_addrs_map = Province::COUNT;
        foreach ($temp_addrs_map as $k => $v) {
            if (isset($temp_addrs[$k])) {

                $temp_addrs_map[$k] += $temp_addrs[$k];
            }
        }
        unset($model, $temp_addrs, $temp_addrs_raw);

        return $temp_addrs_map;
    }


    /**
     * 首页其它数据
     *
     * @return array
     */
    public function addonData()
    {
        $person1 = $this->model;
        $person2 = $this->model;
        $user = $this->user;

        // 总用户数量
        $user_count = $user->count();

        // 总人员数
        $person_count = $person1->where('is_deleted', '0')->count('is_deleted');

        $date_start = date("Y-m-d 00:00:00");
        $date_end = date("Y-m-d 23:59:59");

        // 今日新增人员数量
        $person_today_count = $person2->where('is_deleted', '0')
            ->where('created_at', '>', $date_start)
            ->where('created_at', '<', $date_end)->count('is_deleted');

        unset($person1, $person2, $user);

        return [
            'user_count' => $user_count,
            'person_count' => $person_count,
            'person_today_count' => $person_today_count,
        ];
    }


}
